<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1.字面量创建对象
        var o1 = {
            name: '胖鸡',
            age: 18
        }
        o1.sex = '男'
        o1.say = function () { console.log('我好开心呀') }
        console.log('o1对象：', o1)
        o1.say()
        // 遍历对象属性
        for (key in o1) {
            console.log('键名：', key, '键值：', o1[key])
        }

        // 2.通过函数来模拟
        function Person() {
        }
        var o2 = new Person()
        o2.name = '胖胖'
        o2.age = 22
        o2.say = function () {
            console.log('我是胖胖')
        }
        console.log('o2对象：', o2)
        o2.say()

        function Per(name, age, sex) {
            this.name = name,
                this.age = age,
                this.sex = sex
            this.eat = function () {
                console.log('我还是胖胖')
            }
        }
        var o = new Per('小胖', 18, '男')
        console.log('胖胖弟弟o：', o)
        o.eat()

        // 通过new object创建对象
        var o3 = new Object()
        o3.name = '胖鸡Brave'
        o3.sex = '男'
        o3.age = 19
        o3.say = function () { console.log('我是胖鸡Brave') }
        console.log('o3对象：', o3)
        o3.say()

        // 4.通过显示的构造函数创建对象
        function Dog() {
        }
        Dog.prototype.name = '旺财'
        Dog.prototype.sex = "男"
        Dog.prototype.eat = function () {
            console.log('我喜欢吃')
        }
        var o4 = new Dog()
        console.log('o4函数对象：', o4)
        o4.eat()

        function Cat(name, age) {
            this.name = name,
                this.age = age
        }
        Cat.prototype.say = function () { console.log('我是小猫') }
        var o5 = new Cat('小白', 7)
        console.log('o5对象：', o5)
        o5.say()


    </script>
</body>

</html>